//
//  PEDeviceLoadCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/7.
//

#import "RHDeviceLoadDataCell.h"
@interface RHDeviceLoadDataCell()

//当日充电量
@property (nonatomic ,strong) QMUILabel *chgTodayLab;
//当日放电量
@property (nonatomic ,strong) QMUILabel *dchgTodayLab;
//累计充电量
@property (nonatomic ,strong) QMUILabel *chgTotalLab;
//累计放电量
@property (nonatomic ,strong) QMUILabel *dchgTotalLab;

@end
@implementation RHDeviceLoadDataCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setDic:(NSDictionary *)dic
{
    _dic = dic;
   
    NSString *chg_today = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"chg_today"], @"--")];
    NSString *dchg_today = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"dchg_today"], @"--")];
    NSString *chg_total = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"chg_total"], @"--")];
    NSString *dchg_total = [NSString stringWithFormat:@"%@ (kWh)",ValidNullDataToString(dic[@"dchg_total"], @"--")];
    
    
    self.chgTodayLab.attributedText = [NSString attributeStringWithContent:chg_today attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.dchgTodayLab.attributedText = [NSString attributeStringWithContent:dchg_today attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.chgTotalLab.attributedText = [NSString attributeStringWithContent:chg_total attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
    self.dchgTotalLab.attributedText = [NSString attributeStringWithContent:dchg_total attriFont:kSYSTEMFONT(10) attriColor:kCOLOR_HEX(@"#8B8D92") attriKey:@"(kWh)"];
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,0, 0, 0));
    }];
    
    
    
    //当日 充放电量
    self.chgTodayLab = [[QMUILabel alloc]init];
    self.dchgTodayLab = [[QMUILabel alloc]init];
    self.chgTodayLab.text = @"--";
    self.dchgTodayLab.text = @"--";
    
    UIView *monthView = [self itemWithTypeTitle:@"当日电量".rh_localized subTitle1:@"充电量".rh_localized lab1:self.chgTodayLab subTitle2:@"放电量".rh_localized lab2:self.dchgTodayLab icon:@"kwt_bat_chg_today"];
    [monthView rounded:8 width:1 color:kBorderColor];
    [bgView addSubview:monthView];
    [monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
    }];
    
    //累计 充放电量
    self.chgTotalLab = [[QMUILabel alloc]init];
    self.dchgTotalLab = [[QMUILabel alloc]init];
    self.chgTotalLab.text = @"--";
    self.dchgTotalLab.text = @"--";
    UIView *totalView = [self itemWithTypeTitle:@"累计电量".rh_localized subTitle1:@"累计充电量".rh_localized lab1:self.chgTotalLab subTitle2:@"累计放电量".rh_localized lab2:self.dchgTotalLab icon:@"kwt_bat_chg_total"];
    [totalView rounded:8 width:1 color:kBorderColor];
    [bgView addSubview:totalView];
    [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(monthView);
        make.right.equalTo(monthView);
        make.top.equalTo(monthView.mas_bottom).offset(15);
        make.bottom.mas_offset(0);
    }];
}


//当月数据  累计数据

-(UIView*)itemWithTypeTitle:(NSString*)title  subTitle1:(NSString *)title1 lab1:(QMUILabel*)lab1 subTitle2:(NSString*)title2 lab2:(QMUILabel*)lab2 icon:(NSString*)icon
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView rounded:8 width:1 color:kBorderColor];
    [self.contentView addSubview:bgView];
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#303133")];
    [bgView addSubview:titleLab];
    titleLab.text = title;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    CGFloat itemWidth = bgWidth / 2;
    //左边第一个
    
    UIView *itemView_1 = kView;
    [itemBgView addSubview:itemView_1];
    QMUILabel *subTitleLab_1 = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    subTitleLab_1.text = title1.rh_localized;
    [itemView_1 addSubview:subTitleLab_1];
    [itemView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(itemWidth);
        make.left.mas_offset(0);
        make.top.mas_offset(0);
        make.height.offset(60);
        make.bottom.mas_offset(0);
    }];
    
    
    
    lab1.font = kBOLDSYSTEMFONT(16);
    lab1.textColor = kCOLOR_HEX(@"#303133");
    lab1.textAlignment = NSTextAlignmentLeft;
    [itemView_1 addSubview:lab1];
    
    [subTitleLab_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemView_1.mas_centerY).offset(3);
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
    }];
    
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView_1.mas_centerY).offset(-3);
    }];
    
    //线
    UIView *line = kView;
    line.hidden = YES;
    line.backgroundColor = kCOLOR_HEX(@"#EBEDF0");
    [itemView_1 addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.height.offset(35);
        make.centerY.mas_offset(0);
        make.right.equalTo(itemView_1);
    }];
    
    //右边第二个
    UIView *itemView_2 = kView;
    [itemBgView addSubview:itemView_2];
    QMUILabel *subTitleLab_2 = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
    subTitleLab_2.text = title2.rh_localized;
    [itemView_2 addSubview:subTitleLab_2];
    [itemView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(itemWidth);
        make.left.equalTo(itemView_1.mas_right);
        make.top.mas_offset(0);
        make.height.offset(60);
    }];
    
   
    lab2.font = kBOLDSYSTEMFONT(16);
    lab2.textColor = kCOLOR_HEX(@"#303133");
    lab2.textAlignment = NSTextAlignmentLeft;
    [itemView_2 addSubview:lab2];
    
    [subTitleLab_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(itemView_2.mas_centerY).offset(3);
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
    }];
    
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(50);
        make.right.mas_offset(-3);
        make.bottom.equalTo(itemView_2.mas_centerY).offset(-3);
    }];
    
    return bgView;
}

@end
